package com.zhou.JavaAndroidFirm.model;

import com.zhou.JavaAndroidFirm.app.AppConstant;
import com.zhou.JavaAndroidFirm.base.BaseApplication;
import com.zhou.JavaAndroidFirm.bean.NewsChannelTable;
import com.zhou.JavaAndroidFirm.bean.NewsChannelTableManager;
import com.zhou.JavaAndroidFirm.constract.NewsMainConstract;
import com.zhou.JavaAndroidFirm.utils.ACache;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Administrator
 * @date 2018/7/15 0015
 * @des
 */
public class NewsMainModel implements NewsMainConstract.Model {
    @Override
    public Observable<List<NewsChannelTable>> lodeMineNewsChannels() {
        return Observable.create(new Observable.OnSubscribe<List<NewsChannelTable>>() {
            @Override
            public void call(Subscriber<? super List<NewsChannelTable>> subscriber) {
                ArrayList<NewsChannelTable> newsChannelTables = (ArrayList<NewsChannelTable>) ACache.get(BaseApplication.getAppContext()).getAsObject(AppConstant.CHANNEL_MINE);
                if (null == newsChannelTables) {
                    newsChannelTables = (ArrayList<NewsChannelTable>) NewsChannelTableManager.loadNewsChannelsStatic();
                    ACache.get(BaseApplication.getAppContext()).put(AppConstant.CHANNEL_MINE, newsChannelTables);
                }
                subscriber.onNext(newsChannelTables);
                subscriber.onCompleted();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
